﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.stage;
using Microsoft.Xna.Framework.Content;
using Imaginecup.gameState.temp;
using Imaginecup.factory;
using Microsoft.Xna.Framework;
using Imaginecup.gameState.water.stageMan;
using Imaginecup.util.resourceMan;
using Microsoft.Xna.Framework.Graphics;

namespace Imaginecup.gameState.water
{
    public class WaterState : GameState, IControlStageMan
    {
        private IStageMan m_StageMan;
        private StateStageManID m_CurrentStateStageID;

        private ContentManager m_Content;
        private static WaterState m_WaterState = null;



        private WaterState()
        {
            m_Content = new ContentManager(m_Game.Services);
            m_Content.RootDirectory = "Content";

       
        }

        public static WaterState GetWaterState()
        {
            if (m_WaterState == null)
            {
                m_WaterState = new WaterState();
            }
            return m_WaterState;
        }


        /************************************************************************/
        /* 초기화 되는 값                                                       */
        /************************************************************************/
        protected override void InitState()
        {
            //    m_TrailLayer = new TouchTrailLayer();
            //     ObjectManager objMan = ObjectManager.GetObjectManager();
            //     objMan.Init(new MalariaCompInit());
            //     m_StageMan.Init();
        }

        /************************************************************************/
        /* ContentMan에 리소스 로드(배경,메뉴 리소스)  공통사항                                */
        /************************************************************************/
        protected override void LoadStateResource()
        {
            ResourceMan resourceMan = ResourceMan.GetResourceMan();
            //컨트롤러           
            resourceMan.SetTextureFrame("water_right_point", m_Content.Load<Texture2D>("water\\Controller\\water_right_point"));
            resourceMan.SetTextureFrame("water_left_point", m_Content.Load<Texture2D>("water\\Controller\\water_left_point"));
            resourceMan.SetTextureFrame("water_left_navi", m_Content.Load<Texture2D>("water\\Controller\\water_left_navi"));
            //체력바
            resourceMan.SetTextureFrame("water_life_bar", m_Content.Load<Texture2D>("water\\Controller\\water_life_bar"));
            resourceMan.SetTextureFrame("water_life_base", m_Content.Load<Texture2D>("water\\Controller\\water_life_base"));
            //흡수,발사 컨트롤
            resourceMan.SetTextureFrame("water_attack_1", m_Content.Load<Texture2D>("water\\Controller\\water_attack_1"));
            resourceMan.SetTextureFrame("water_attack_2", m_Content.Load<Texture2D>("water\\Controller\\water_attack_2"));
            //기본 배경
            //resourceMan.SetTextureFrame("BaseBackground", m_Content.Load<Texture2D>("water\\Background\\Background"));
            resourceMan.SetTextureFrame("BaseBackground", m_Content.Load<Texture2D>("water\\12_31\\OldBackground"));
            //파도
            resourceMan.SetTextureFrame("WaveBackground", m_Content.Load<Texture2D>("water\\Background\\blue_background_wave"));

            m_StageMan.LoadResource();

        }


        /************************************************************************/
        /* 리소스해제                                                                     */
        /************************************************************************/
        protected override void UnloadResource()
        {
           
            m_StageMan.Release();
            ResourceMan.GetResourceMan().Release();
            //SoundMan.GetSoundMan().ReleaseSound();
            m_Content.Unload();
        }

        public override void AddStateEvent()
        {
            if (m_GameStateMan.GetTopState() != this)
            {
                this.Enabled = false;
            }
            else
                this.Enabled = true;

            base.AddStateEvent();
        }


        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {

            IStageLoop stg = m_StageMan.GetStage();

            if (m_StageMan.IsClear())
            {
                ChoiceMap choiceMap = (ChoiceMap)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_ChoiceMap);

                ChangeState(choiceMap);
                choiceMap.ClearState(m_CurrentStateStageID);
                return;
                // ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_TitleMenu));
            }

            stg.Update(gameTime);



            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            IStageLoop stg = m_StageMan.GetStage();
            stg.Draw(gameTime, m_GraphicData.GetSpriteBatch());

            base.Draw(gameTime);

        }


        public void SetStageManID(StateStageManID id)
        {
            m_StageMan = (WaterStageMan)WaterStageManFactory.GetMalariaStageManFactory().GetObject(id);
            m_CurrentStateStageID = id;
        }
    }
}
